package com.atguigu.service.impl;

import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.UserFollow;
import com.atguigu.entity.vo.UserFollowVo;
import com.atguigu.mapper.UserFollowMapper;
import com.atguigu.service.UserFollowService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author:hx
 * @DateTime:2022/9/30 15:05
 * @Description:
 */
@DubboService
public class UserFollowServiceImpl extends BaseServiceImpl<UserFollow> implements UserFollowService {
    @Autowired
    private UserFollowMapper userFollowMapper;
    @Override
    public BaseMapper<UserFollow> getBaseMapper() {
        return userFollowMapper;
    }

    @Override
    public boolean getFollowByUserIdAndHouseId(Long userId, Long houseId) {
        UserFollow userFollow = userFollowMapper.getFollowByUserIdAndHouseId(userId, houseId);
        if (userFollow != null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void addFollow(Long userId, Long houseId) {
        //先根据条件进行查询,然后根据结果判断
        //不能使用getFollowByUserIdAndHouseId()方法进行查询,因为不管is_deleted字段是否为1或0都要查询
        UserFollow userFollow = userFollowMapper.findFollowByUserIdAndHouseId(userId,houseId);
        if (userFollow != null){
            //关注过,但取消了
            userFollow.setIsDeleted(0);
            userFollowMapper.update(userFollow);
        }else {
            //没关注过
            userFollow = new UserFollow();
            userFollow.setUserId(userId);
            userFollow.setHouseId(houseId);
            userFollowMapper.insert(userFollow);
        }
    }

    @Override
    public void cancelFollow(Long userId, Long houseId) {
        userFollowMapper.cancelFollow(userId,houseId);
    }

    @Override
    public PageInfo<UserFollowVo> findUserFollowVoPage(Integer pageNum, Integer pageSize, Long userId) {
        PageHelper.startPage(pageNum,pageSize);
        Page<UserFollowVo> list = userFollowMapper.findUserFollowVoPage(userId);
        return new PageInfo<>(list,10);
    }

    @Override
    public void cancelFollowById(Long id) {
        userFollowMapper.cancelFollowById(id);
    }
}

